/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.modules.sys.utils;

import java.io.Serializable;
import java.util.List;

import cn.knet.sp.ncrm.common.utils.CacheUtils;
import cn.knet.sp.ncrm.common.utils.SpringContextHolder;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.sys.customerfeature.dao.CustomerFeatureDao;
import cn.knet.sp.ncrm.modules.sys.customerfeature.entity.CustomerFeature;
import cn.knet.sp.ncrm.modules.sys.customerstatus.dao.CustomerStatusDao;
import cn.knet.sp.ncrm.modules.sys.customerstatus.entity.CustomerStatus;
import cn.knet.sp.ncrm.modules.sys.customerstatusdetail.dao.CustomerStatusDetailDao;
import cn.knet.sp.ncrm.modules.sys.customerstatusdetail.entity.CustomerStatusDetail;
import cn.knet.sp.ncrm.modules.sys.customertype.dao.CustomerTypeDao;
import cn.knet.sp.ncrm.modules.sys.customertype.entity.CustomerType;
import cn.knet.sp.ncrm.modules.sys.user.entity.User;

/**
 * 客户特点 / 类型 / 详情 查询创建工具
 *
 * @author zhangbin
 * @version 2015 /08/26 15:33:24
 */
public class CustomerUtils {

	public static final String CUSTOMER_CACHE = "customerCache";
	public static final String FEATURE_CACHE_NAME = "feature_name_";
	public static final String FEATURE_CACHE_ID = "feature_id_";
	public static final String TYPE_CACHE_NAME = "type_name_";
	public static final String TYPE_CACHE_ID = "type_id_";
	public static final String STATUS_DETAIL_CACHE_ID = "detail_id_";
	public static final String STATUS_CACHE_ID = "status_id_";
	public static final String STATUS_CACHE_NAME = "status_name_";
	
	//add by jief 20170212
	public static final String USER_TYPE_OFFICER="4";
	public static final String USER_TYPE_MANAGER="2";
	public static final String USER_TYPE_USER="3";
	public static final String USER_TYPE_ADMIN="1";

	private static CustomerFeatureDao customerFeatureDao = SpringContextHolder.getBean(CustomerFeatureDao.class);
	private static CustomerTypeDao customerTypeDao = SpringContextHolder.getBean(CustomerTypeDao.class);
	private static CustomerStatusDao customerStatusDao = SpringContextHolder.getBean(CustomerStatusDao.class);
	private static CustomerStatusDetailDao customerStatusDetailDao = SpringContextHolder.getBean(CustomerStatusDetailDao.class);

	/************************************
	 * 客户特点片段
	 ************************************/
	public static String getFeatureId(String name) {
		CustomerFeature feature = getFeatureByName(name);
		if (feature != null) {
			return feature.getId();
		}
		return null;
	}

	/**
	 * 根据名称 查找客户特点,缓存读取->数据库查询->不存在则创建
	 *
	 * @param name name
	 * @return CustomerFeature 对象
	 * @author zhangbin
	 * @date 2015 /08/26 16:34:27
	 */
	public static CustomerFeature getFeatureByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		name = StringUtils.trim(name);
		int nameCode = name.hashCode();
		CustomerFeature enitity = (CustomerFeature) CacheUtils.get(CUSTOMER_CACHE, FEATURE_CACHE_NAME + nameCode);
		if (enitity == null) {
			enitity = new CustomerFeature();
			enitity.setName(name);
			enitity = customerFeatureDao.getByEntity(enitity);
			if (enitity == null) {
				enitity = new CustomerFeature();
				enitity.setName(name);
				enitity.preInsert();//初始化部分参数
				enitity.setDateCreated(new java.util.Date());
				enitity.setLastUpdated(new java.util.Date());
				if (enitity.getCreateBy() == null || enitity.getUpdateBy() == null) {
					enitity.setCreateBy(new User("1")); //默认为系统用户
					enitity.setUpdateBy(new User("1"));
				}
				customerFeatureDao.insert(enitity);    //抛弃service仅能用dao
			}
			CacheUtils.put(CUSTOMER_CACHE, FEATURE_CACHE_NAME + nameCode, enitity);
			CacheUtils.put(CUSTOMER_CACHE, FEATURE_CACHE_ID + enitity.getId(), enitity);
			//写入缓存
		}
		return enitity;
	}

	/**
	 * 根据ID查询出客户特点对象
	 * @param id
	 * @return CustomerFeature
	 */
	public static CustomerFeature getFeatureById(String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}
		id = StringUtils.trim(id);
		CustomerFeature enitity = (CustomerFeature) CacheUtils.get(CUSTOMER_CACHE, FEATURE_CACHE_ID + id);
		if (enitity == null) {
			enitity = customerFeatureDao.get(id);
			if (enitity != null) {
				CacheUtils.put(CUSTOMER_CACHE, FEATURE_CACHE_NAME + enitity.getName().hashCode(), enitity);
				CacheUtils.put(CUSTOMER_CACHE, FEATURE_CACHE_ID + enitity.getId(), enitity);
			}
		}
		return enitity;
	}

	/**
	 * 查询全部没有删除的类型
	 * @author zhangbin
	 * @date 2015 /08/26 18:24:31
	 * @return the list
	 */
	public static List<CustomerFeature> listFeature(){
		CustomerFeature entity = new CustomerFeature();
		entity.setDelFlag("0");
		return customerFeatureDao.findAllList(entity);
	}


	/**
	 * 清除某个特点的缓存
	 * @author zhangbin
	 * @date 2015 /08/27 11:26:42
	 * @param customerFeature the customer feature
	 */
	public static void clearFeature(CustomerFeature customerFeature) {
		CacheUtils.remove(CUSTOMER_CACHE, FEATURE_CACHE_NAME + customerFeature.getName().hashCode());
		CacheUtils.put(CUSTOMER_CACHE, FEATURE_CACHE_ID + customerFeature.getId());
	}

	/************************************
	 * 客户类型片段
	 ************************************/
	/**
	 * 根据客户类型名称 获取ID
	 *
	 * @param name the name
	 * @return id
	 */
	public static String getTypeId(String name) {
		CustomerType type = getTypeByName(name);
		if (type != null) {
			return type.getId();
		}
		return null;
	}

	/**
	 * 根据名称获取客户类型
	 * @param name the name
	 * @return CustomerType
	 */
	public static CustomerType getTypeByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		name = StringUtils.trim(name);
		int nameCode = name.hashCode();
		System.err.println("准备从缓存读取出类型对象id=" + TYPE_CACHE_NAME + nameCode);
		CustomerType enitity = (CustomerType) CacheUtils.get(CUSTOMER_CACHE, TYPE_CACHE_NAME + nameCode);
		System.err.println((enitity instanceof Serializable));
		if (enitity == null) {
			System.err.println("没有类型对象在缓存中");
			enitity = new CustomerType();
			enitity.setName(name);
			enitity = customerTypeDao.getByEntity(enitity);
			if (enitity == null) {
				enitity = new CustomerType();
				enitity.setName(name);
				enitity.preInsert();//初始化部分参数
				enitity.setDateCreated(new java.util.Date());
				enitity.setLastUpdated(new java.util.Date());
				if (enitity.getCreateBy() == null || enitity.getUpdateBy() == null) {
					enitity.setCreateBy(new User("1")); //默认为系统用户
					enitity.setUpdateBy(new User("1"));
				}
				customerTypeDao.insert(enitity);    //创建
			}
			CacheUtils.put(CUSTOMER_CACHE, TYPE_CACHE_NAME + nameCode, enitity);
			CacheUtils.put(CUSTOMER_CACHE, TYPE_CACHE_ID + enitity.getId(), enitity);
			//写入缓存
		}else{
			System.err.println("恭喜这次是从缓存里取得的");
		}
		return enitity;
	}

	/**
	 * 根据ID查询出客户类型对象
	 * @param id
	 * @return CustomerType
	 */
	public static CustomerType getTypeById(String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}
		id = StringUtils.trim(id);
		CustomerType enitity = (CustomerType) CacheUtils.get(CUSTOMER_CACHE, TYPE_CACHE_ID + id);
		if (enitity == null) {
			enitity = customerTypeDao.get(id);
			if (enitity != null) {
				CacheUtils.put(CUSTOMER_CACHE, TYPE_CACHE_NAME + enitity.getName().hashCode(), enitity);
				CacheUtils.put(CUSTOMER_CACHE, TYPE_CACHE_ID + enitity.getId(), enitity);
			}
		}
		return enitity;
	}

	/**
	 * 查询全部类型
	 * @author zhangbin
	 * @date 2015 /08/27 11:28:08
	 * @return the list
	 */
	public static List<CustomerType> listType(){
		CustomerType entity = new CustomerType();
		entity.setDelFlag("0");
		return customerTypeDao.findAllList(entity);
	}

	/**
	 * 清除类型缓存
	 * @author zhangbin
	 * @date 2015 /08/27 11:28:56
	 * @param customerType the customer type
	 */
	public static void clearType(CustomerType customerType) {
		CacheUtils.remove(CUSTOMER_CACHE, TYPE_CACHE_NAME + customerType.getName().hashCode());
		CacheUtils.put(CUSTOMER_CACHE, TYPE_CACHE_ID + customerType.getId());
	}

	/************************************
	 * 客户状态片段
	 ************************************/

	public static List<CustomerStatus> listStatus(){
		CustomerStatus entity = new CustomerStatus();
		entity.setDelFlag("0");
		return customerStatusDao.findAllList(entity);
	}

	public static List<CustomerStatusDetail> listStatusDetail(String id){
		CustomerStatusDetail enitiy = new CustomerStatusDetail();
		enitiy.setCustomerStatusId(id);
		enitiy.setDelFlag("0");
		return customerStatusDetailDao.findList(enitiy);
	}

	/**
	 * 根据客户状态名称查询出ID
	 * @author zhangbin
	 * @date 2015 /09/11 10:52:02
	 * @param name the name
	 * @return the string
	 */
	public static String getStatusIdByName(String name){
		CustomerStatus entity = getStatusByName(name);
		if(entity==null){
			return null;
		}else{
			return entity.getId();
		}
	}

	/**
	 * 根据客户状态名称查询出 客户状态对象
	 * @author zhangbin
	 * @date 2015 /09/11 10:52:02
	 * @param name the name
	 * @return customer status
	 */
	public static CustomerStatus getStatusByName(String name){
		if (StringUtils.isBlank(name)) {
			return null;
		}
		name = StringUtils.trim(name);
		int nameCode = name.hashCode();
		CustomerStatus enitity = (CustomerStatus) CacheUtils.get(CUSTOMER_CACHE, STATUS_CACHE_NAME + nameCode);
		if (enitity == null) {
			enitity = new CustomerStatus();
			enitity.setContent(name);
			enitity = customerStatusDao.getByEntity(enitity);
			if (enitity != null) {
				CacheUtils.put(CUSTOMER_CACHE, STATUS_CACHE_NAME + nameCode, enitity);
				CacheUtils.put(CUSTOMER_CACHE, STATUS_CACHE_ID + enitity.getId(), enitity);
			}
		}
		return enitity;
	}

	/**
	 * 从缓存读取客户状态
	 * @author zhangbin
	 * @date 2015 /09/06 16:46:02
	 * @param id the id
	 * @return the customer status
	 */
	public static CustomerStatus getStatus(String id){
		if (StringUtils.isBlank(id)) {
			return null;
		}
		id = StringUtils.trim(id);
		CustomerStatus enitity = (CustomerStatus) CacheUtils.get(CUSTOMER_CACHE, STATUS_CACHE_ID + id);
		if (enitity == null) {
			enitity = customerStatusDao.get(id);
			if (enitity != null) {
				int nameCode = enitity.getContent().hashCode();
				CacheUtils.put(CUSTOMER_CACHE, STATUS_CACHE_ID + enitity.getId(), enitity);
				CacheUtils.put(CUSTOMER_CACHE, STATUS_CACHE_NAME + nameCode, enitity);
			}
		}
		return enitity;
	}

	/**
	 * 根据明细ID获取
	 * @author zhangbin
	 * @date 2015 /09/06 16:48:19
	 * @param id the id
	 * @return the customer status detail
	 */
	public static CustomerStatusDetail getStatusDetail(String id){
		if (StringUtils.isBlank(id)) {
			return null;
		}
		id = StringUtils.trim(id);
		CustomerStatusDetail enitity = (CustomerStatusDetail) CacheUtils.get(CUSTOMER_CACHE, STATUS_DETAIL_CACHE_ID + id);
		if (enitity == null) {
			enitity = customerStatusDetailDao.get(id);
			if (enitity != null) {
				CacheUtils.put(CUSTOMER_CACHE, STATUS_DETAIL_CACHE_ID + enitity.getId(), enitity);
			}
		}
		return enitity;
	}

	/**
	 * 清除状态缓存
	 * @author zhangbin
	 * @date 2015 /08/27 11:28:56
	 * @param customerStatus the customer status
	 */
	public static void clearStatus(CustomerStatus customerStatus) {
		if(customerStatus!=null&&customerStatus.getContent()!=null){
			int nameCode = customerStatus.getContent().hashCode();
			CacheUtils.put(CUSTOMER_CACHE, STATUS_CACHE_NAME + nameCode);
		}
		CacheUtils.put(CUSTOMER_CACHE, STATUS_CACHE_ID + customerStatus.getId());

	}

	/**
	 * 清除状态明细缓存
	 * @author zhangbin
	 * @date 2015 /09/06 16:49:38
	 * @param customerStatusDetail the customer status detail
	 */
	public static void clearDetail(CustomerStatusDetail customerStatusDetail) {
		CacheUtils.put(CUSTOMER_CACHE, STATUS_DETAIL_CACHE_ID + customerStatusDetail.getId());
	}

}
